/**
 * Created by Delicate on 2018/1/3.
 */
import React from 'react';
import {
    StyleSheet,
    View,
    ScrollView,

} from 'react-native';

import NavBar from '../../components/NavBarView'
import {
    DefaultField,
    DefaultLabel,
} from '../../components/Form'
import SystemService from '../../services/System'

let _self;

class Views extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            list: []
        }
        _self = this;
    }

    componentWillMount() {

    }

    componentDidMount() {
        this.load();
    }

    async load() {
        const {result,message,data} = await SystemService.getAssetType();
        if(result){
            const list = data.assetTypes;
            _self.setState({list})
        }else{
            Toast.show(message)
        }
    }

    select(item) {
        if (_self.props.navigation.state.params && _self.props.navigation.state.params.result) {
            _self.props.navigation.state.params.result({
                asset_type: item.asset_type_name,
                asset_type_id: item.id,
                selectedItem: item
            })
            _self.props.navigation.goBack()
        }
    }

    render() {
        return (
            <View style={base.baseOuter}>
                <NavBar host={this} title="选择资产类型"/>
                <ScrollView style={styles.container}>
                    <View style={styles.box}>
                        {
                            this.state.list.map(item=>{
                                return(
                                    <DefaultField key={item.id} text={item.asset_type_name}>
                                        <DefaultLabel onPress={() => this.select(item)}
                                                      rightIcon={require('../../sources/icon_global_go.png')}/>
                                    </DefaultField>
                                )
                            })
                        }
                        <DefaultField last={true} text="其他">
                            <DefaultLabel onPress={() => this.select({
                                asset_type_name: '其他',
                                id: '',
                                selectedItem: {}
                            })} rightIcon={require('../../sources/icon_global_go.png')}/>
                        </DefaultField>
                    </View>
                </ScrollView>
            </View>
        )
    }
}

const styles = StyleSheet.create({
    container: {
        backgroundColor: layout.MAIN_BACKGROUND_COLOR
    },
    box: {
        backgroundColor: '#fff'
    }
});

//路由
const AssetTypeList = {
    screen: Views,
    navigationOptions: {
        header: null,
    },
    name: 'AssetTypeList',
    sign: true
};

export default AssetTypeList;